<?php

require_once "Spider.php";
require_once "RedisDB.php";

Co\run(function(){

    $spider =  new Spider();
    $redis = new RedisDB();
    $chan = new Swoole\Coroutine\Channel(12);

    $task_worker_num = 2;
    $uid = "yuanziok";

    echo "################################################################",PHP_EOL;
    echo "【启动爬虫】",PHP_EOL;
    echo "################################################################",PHP_EOL;


    // 生产
    Swoole\Coroutine::create(function() use ($spider,$redis,$chan,$uid) {

// $list = $spider->crawlList($uid);
// if(!empty($list)){
//     foreach($list as $uid){
//         $res = $redis->pushUid($uid);
//     }
// }

        // 开始爬
        $count = $redis->getSize();
        while(!empty($count)){
            $uid = $redis->popUid();
            if($uid){
                // 爬取页面，获取粉丝和关注列表
                $list = $spider->crawlList($uid);

                // 更改uid的状态，避免循环重复爬取页面
                $redis->keySetExists($uid);
                    
                foreach($list as $uid){
                    $stat = $redis->keyIsExists($uid);   // 存在：true  不存在：false
                    if(!$stat){ 
                        $res = $redis->pushUid($uid);          // 将uid放入redis队列
                    }
                }
                // 将uid放入channel
                // 放在后面，可以稍微减缓生产速度大于消费速度
                $rs = $chan->push($uid, $timeout = -1);    
                $length = $chan->length();      
                if($rs){
                    echo "【加入了{$uid}任务，任务总数：{$length}】", PHP_EOL;
                }else{
                    // $why = $chan->errCode;
                    $why = swoole_strerror(swoole_last_error(), 9);
                    echo "【加入了任务失败，原因：{$why}】", PHP_EOL;
                }
            }

            $count = $redis->getSize();
            echo "----------------------- 【队列中：{$count}】 -----------------------", PHP_EOL,PHP_EOL;
        }
    });

    // 消费
    for($i=0; $i<$task_worker_num; $i++){
        
            Swoole\Coroutine::create(function() use ($spider,$redis,$chan) {
                while(true){
                    $uid = $chan->pop();
                    if($uid){
                        $info = $spider->crawlInfo($uid);
                        $res = $redis->saveInfo($uid, $info);
                        if($res){
                            echo "【保存{$uid}信息：成功】", PHP_EOL;
                        }else{
                            echo "【保存{$uid}信息：失败】", PHP_EOL;
                        }
                    }
                    echo "************************************************************", PHP_EOL,PHP_EOL;
                }
            });  
    }

});
